﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using Microsoft.VisualBasic;
namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        IDS [] id = new IDS[4400];
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Help help = new Help();
            help.Show();
            MessageBox.Show("请查看弹出的提示窗体");
            listView1.Items.Clear();
            listView1.Columns.Clear();
            listView2.Items.Clear();
            listView2.Columns.Clear();
            listView1.Columns.Add("身份证件号");
            listView1.Columns[0].Width = 190;
            listView2.Columns.Add("发卡单位");
            listView2.Columns.Add("籍贯");
            listView2.Columns.Add("生日");
            listView2.Columns.Add("性别");
            listView2.Columns.Add("生肖");
            listView2.Columns.Add("星座");
            listView2.Columns[0].Width = 100;
            listView2.Columns[1].Width = 100;
            listView2.Columns[2].Width = 100;
            listView2.Columns[3].Width = 50;
            listView2.Columns[4].Width = 50;
            listView2.Columns[5].Width = 50;
            //string file = System.AppDomain.CurrentDomain.BaseDirectory;
            StreamReader sr = new StreamReader("inif.csv",Encoding.Default); 
            string str = "";
            string a, b, c, d, e2;
            string[] str_temp;
            int index = 0;
            while (!sr.EndOfStream)
            {
                str = sr.ReadLine().Trim();
                str_temp = str.Split(',');
                a = str_temp[0].Trim();
                b = str_temp[1].Trim();
                c = str_temp[2].Trim();
                d = str_temp[3].Trim();
                e2 = str_temp[4].Trim();
                id[index++] = new IDS(a, b, c, d, e2);
            }
            sr.Close();
        }

        private void 批量ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog fs = new OpenFileDialog();
            if (fs.ShowDialog() == DialogResult.OK)
            {
                StreamReader sr = new StreamReader(fs.FileName);

                while (!sr.EndOfStream)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = sr.ReadLine();
                    listView1.Items.Add(lvi);
                }
                sr.Close();
            }
        }
        public struct IDS{
            public string dmmc;
            public string dmzm;
            public string dmbz;
            public string dmxh;
            public string dmmcl;
            public IDS(string a, string b, string c, string d, string e)
            {
                this.dmmc = a;
                this.dmzm = b;
                this.dmbz = c;
                this.dmxh = d;
                this.dmmcl = e;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            listView2.Items.Clear();
            string str = textBox1.Text.ToString();
            string [] sour=str.Split(';');
            foreach (string s in sour )
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Text = s;
                listView1.Items.Add(lvi);
            }
        }

        private void 开始ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool all_pass = true;
            foreach (ListViewItem lvi in listView1.Items)
            {
                string s = lvi.Text;
                if (s.Length != 18)
                {
                    all_pass = false;
                    lvi.ForeColor = System.Drawing.Color.Red;
                }
            }
            if (all_pass != true)
            {
                MessageBox.Show("你输入的身份证号码有误，已用红色标注，请修改!!");
                return ;
            }
            foreach (ListViewItem lvi in listView1.Items)
            {
                string keys = lvi.Text;
                string dmzm = keys.Substring(0, 6);
                int sex =int.Parse(keys.Substring(16, 1)) ;
                string birth_y=keys.Substring(6,4);
                string birth_m=keys.Substring(10,2);
                string birth_d=keys.Substring(12,2);
                ListViewItem l = new ListViewItem();
                bool flag = false;
                foreach(IDS i in id)
                {
                    if (i.dmzm.Equals(dmzm))
                    {
                        flag = true;
                        l.Text = i.dmmc+"公安局";
                        l.SubItems.Add(i.dmmcl);
                        l.SubItems.Add(birth_y + "年" + birth_m + "月" + birth_d + "日");
                        if (sex%2==0)
                        {
                            l.SubItems.Add("女");
                        } //if
                        else
                        {
                            l.SubItems.Add("男");
                           
                        }//else
                        l.SubItems.Add(sx(birth_y));
                        l.SubItems.Add(xz(birth_m, birth_d));
                        break;
                    }//if 
                }
                if (flag)
                {
                    listView2.Items.Add(l);
                }
                else
                {
                    l.Text = "notfound";
                    l.SubItems.Add("notfound");
                    l.SubItems.Add("notfound");
                    l.SubItems.Add("notfound");
                    l.SubItems.Add("notfound");
                    l.SubItems.Add("notfound");
                    listView2.Items.Add(l);
                }  
            }
        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog savefile = new SaveFileDialog();
            savefile.Filter = "csv.files(*.csv)|*.txt)";
            savefile.FilterIndex = 2;
            savefile.RestoreDirectory = true;
            if (savefile.ShowDialog() == DialogResult.OK)
            {
                StreamWriter sw = new StreamWriter(savefile.FileName,true);
                sw.WriteLine("发卡单位      "+"籍贯          "+"生日       " +"性别    ");
                foreach(ListViewItem  l in listView2.Items)
                {
                    sw.WriteLine(l.Text + "       " + l.SubItems[0].Text + l.SubItems[1].Text + l.SubItems[2].Text);
                }
                sw.Close();
                MessageBox.Show("文件保存成功,路径:"+savefile.FileName);
            }
        }
        public string sx(string y)
        {
             uint temp_y =uint.Parse(y);
             temp_y = (temp_y - 1900) % 12;
            switch (temp_y)
            {
                case 0:
                    return "鼠";
                    break;
                case 1:
                    return "牛";
                    break;
                case 2:
                    return "虎";
                    break;
                case 3:
                    return "兔";
                    break;
                case 4:
                    return "龙";
                    break;
                case 5:
                    return "蛇";
                    break;
                case 6:
                    return "马";
                    break;
                case 7:
                    return "羊";
                    break;
                case 8:
                    return "猴";
                    break;
                case 9:
                    return "鸡";
                    break;
                case 10:
                    return "狗";
                    break;
                case 11:
                    return "猪";
                    break;
                default:
                    return "not found";
                    break;
            }
            //鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪
        }
        public string xz(string x1, string x2)
        {
            if (x1 == "01")
            {
                if (int.Parse(x2)<=19)
                    return "水瓶";
                else
                    return "水瓶";
            }
          
            else if(x1=="02")
            {
                 if (int.Parse(x2)<=19)
                    return "水瓶";
                else
                    return "双鱼";
            }
              else if(x1=="03")
            {
               if (int.Parse(x2)<=20)
                    return "双鱼";
                else
                    return "白羊";
            }
              else if(x1=="04")
            {
                 if (int.Parse(x2)<=20)
                    return "白羊";
                else
                    return "金牛";
            }
              else if(x1=="05")
            {
                 if (int.Parse(x2)<=20)
                    return "金牛";
                else
                    return "双子";
            }
              else if(x1=="06")
            {
                 if (int.Parse(x2)<=21)
                    return "双子";
                else
                    return "巨蟹";
            }
              else if(x1=="07")
            {                 
                  if (int.Parse(x2)<=22)
                    return "巨蟹";
                else
                    return "狮子";
            }
              else if(x1=="08")
            {         
                  if (int.Parse(x2)<=22)
                    return "狮子";
                else
                    return "处女";
            }
              else if(x1=="09")
            {
                if (int.Parse(x2)<=22)
                    return "处女";
                else
                    return "天平";
            }
              else if(x1=="10")
            {
                if (int.Parse(x2)<=22)
                    return "天平";
                else
                    return "天蝎";
            }
              else if(x1=="11")
            {      
                  if (int.Parse(x2)<=21)
                    return "天蝎";
                else
                    return "射手";
            }
            else 
            {
                if (int.Parse(x2)<=21)
                    return "射手";
                else
                    return "魔羯";
            }
        }
        private void listView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Point pos = e.Location;
            string str = Microsoft.VisualBasic.Interaction.InputBox("修改", "重新输入", "在这里输入", 100, 100);
            for (int i = 0; i < listView1.Items.Count;i++ )
            {
                Rectangle rect=listView1.GetItemRect(i);
                if ((pos.X > rect.Left) && (pos.X < rect.Right) && (pos.Y > rect.Top) && (pos.Y < rect.Bottom))
                {
                    listView1.Items[i].Text = str;
                }
            }
        }
    }
}
